// +----------------------------------------------------------------------
// | CmsWing [ 美业管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 psi All rights reserved.
// +----------------------------------------------------------------------
// | Author: huang <2374266244@qq.com>
// +----------------------------------------------------------------------
'use strict';
import Base from './base.js';

export default class extends Base {
  /**
   * 获取固定时间的收支情况
   * @return {Promise} []
   */
	async getPayInOutAction(){
		let data = this.get()
		let where = {}
		where['departmentid'] = this.shop_userinfo.departmentid
		where['addtime'] = {'>=': data.begintime, '<': data.endtime}
		let info = await this.model('or_shop_accountlog')
		.field('sum(money) as money, type, ordertype')
		.where(where)
		.group('ordertype')
		.select()
		return this.json(info)
	}


	/*
	 * 获取业绩和提成的发放情况
	 *
	 */
	async getYejiTichengAction () {
		let data = this.get()
		let where = {}
		where['shopid'] = this.shop_userinfo.departmentid
		where['addtime'] = {'>=': data.begintime, '<': data.endtime}
		let departmentid = this.shop_userinfo.departmentid
		let info = await this.model('or_shop_yejiticheng')
		.field('sum(xianjinyeji) as allxy, sum(kahaoyeji) as allky, sum(xianjinticheng) as allxt, sum(kahaoticheng) as allkt, sum(shougongticheng) as allst, sum(qitaticheng) as allqt')
		.where(where)
		.select()
		if (info.length) {
			info = info[0]
		}
		return this.json(info)
	}

	/*
	 * 获取客户各项消费记录排行
	 */
	async getGuestxiaofeiAction () {
		let data = this.get()
		let where = {}
		where['a.shopid'] = this.shop_userinfo.departmentid
		where['a.addtime'] = {'>=': data.begintime, '<': data.endtime}
		let info = await this.model('or_shop_guest_accountlog')
		.alias('a')
		.join('left join `think_or_guest` g on a.guestid = g.id')
		.field('sum(a.balance) as balance, sum(a.giftbalance) as giftbalance, g.name, g.headurl, g.id')
		.where(where)
		.group('guestid')
		.select()
		return this.json(info)
	}


	/*
	 * 获取客户的卡耗现金消费情况（从订单中获取）
	 */
	async getGuestOrderXiaofeiAction () {
		let data = this.get()
		let where = {}
		where['o.departmentid'] = this.shop_userinfo.departmentid
		where['o.addtime'] = {'>=': data.begintime, '<': data.endtime}
		let info = await this.model('or_shop_order')
		.alias('o')
		.join('left join `think_or_guest` g on o.guestid = g.id')
		.field('sum(o.recemoney) as recemoney, sum(o.cardreduce) as cardreduce, sum(o.usebalance) as balance, sum(o.usegiftbalance) as giftbalance, g.name, g.headurl, g.id')
		.where(where)
		.group('o.guestid')
		.select()
		return this.json(info)
	}
	
	
	

	
}